import { ref, computed } from 'vue';
import { useUserStore } from '@/store/user';

/**
 * 用户身份管理组合式函数
 */
export function useUserIdentity() {
  const userStore = useUserStore();
  
  // 响应式状态
  const loading = ref(false);
  
  // 计算属性
  const isLoggedIn = computed(() => userStore.isLoggedIn);
  const userType = computed(() => userStore.userType);
  const userTypeText = computed(() => userStore.userTypeText);
  const isPilot = computed(() => userStore.isPilot);
  const isOrganization = computed(() => userStore.isOrganization);
  const isApprovedPilot = computed(() => userStore.isApprovedPilot);
  const isApprovedOrganization = computed(() => userStore.isApprovedOrganization);
  const canAcceptTask = computed(() => userStore.canAcceptTasks);
  const canPublishTask = computed(() => userStore.canPublishTasks);
  const pilotProfile = computed(() => userStore.pilotProfile);
  const organizationInfo = computed(() => userStore.organizationInfo);
  
  /**
   * 初始化用户身份信息
   */
  const initUserIdentity = async () => {
    if (!userStore.isLoggedIn) {
      console.log('用户未登录，跳过身份初始化');
      return false;
    }
    
    loading.value = true;
    try {
      const identity = await userStore.fetchUserIdentity();
      
      // 添加调试信息
      console.log('=== useUserIdentity 状态检查 ===');
      console.log('userStore.isOrganization:', userStore.isOrganization);
      console.log('userStore.isApprovedOrganization:', userStore.isApprovedOrganization);
      console.log('isOrganization.value:', isOrganization.value);
      console.log('isApprovedOrganization.value:', isApprovedOrganization.value);
      
      return !!identity;
    } catch (error) {
      console.error('初始化用户身份失败:', error);
      return false;
    } finally {
      loading.value = false;
    }
  };
  
  /**
   * 检查权限并显示提示
   */
  const checkPermission = (action: string): boolean => {
    const hasPermission = userStore.checkPermission(action);
    if (!hasPermission) {
      userStore.showPermissionAlert(action);
    }
    return hasPermission;
  };
  
  /**
   * 检查接单权限
   */
  const checkAcceptTaskPermission = (): boolean => {
    return checkPermission('accept_task');
  };
  
  /**
   * 检查发布任务权限
   */
  const checkPublishTaskPermission = (): boolean => {
    return checkPermission('publish_task');
  };
  
  /**
   * 刷新用户身份信息
   */
  const refreshUserIdentity = async () => {
    loading.value = true;
    try {
      await userStore.fetchUserIdentity();
    } catch (error) {
      console.error('刷新用户身份失败:', error);
    } finally {
      loading.value = false;
    }
  };
  
  /**
   * 登出
   */
  const logout = async () => {
    try {
      userStore.clearLoginInfo();
      uni.navigateTo({
        url: '/pages/logon/logon'
      });
    } catch (error) {
      console.error('登出失败:', error);
    }
  };
  
  return {
    // 状态
    loading,
    
    // 计算属性
    isLoggedIn,
    userType,
    userTypeText,
    isPilot,
    isOrganization,
    isApprovedPilot,
    isApprovedOrganization,
    canAcceptTask,
    canPublishTask,
    pilotProfile,
    organizationInfo,
    
    // 方法
    initUserIdentity,
    checkPermission,
    checkAcceptTaskPermission,
    checkPublishTaskPermission,
    refreshUserIdentity,
    logout
  };
}
